package Login.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(urlPatterns = "/*")
public class AutoLoginFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)req;
        HttpServletResponse response = (HttpServletResponse) resp;
        System.out.println(request.getSession());
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        CharacterRequest characterRequest = new CharacterRequest(request);
        chain.doFilter(characterRequest, response);
    }

    class CharacterRequest extends HttpServletRequestWrapper {
        private HttpServletRequest request;
        public CharacterRequest(HttpServletRequest request) {
            super(request);
            this.request = request;
        }
    }

    @Override
    public void destroy() {

    }
}
